﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleCount
{
    class Program
    {
        delegate void Count (params decimal[] num);
        static void doCounting(decimal[] num, Count count)
        {
            count(num);
        }
        static void Main(string[] args)
        {
            if (args.Length >= 1 && (args[0].ToLower().Equals("--help") | args[0].ToLower().Equals("help")))
                PrintHelp();

            if (args.Length < 2)
            {
                Console.WriteLine("没有收到系统传来的有效参数");
                Environment.Exit(1);
            }

            Operactor operactor = 0;

            try
            {
                operactor = (Operactor)Enum.Parse(typeof(Operactor), args[0], true);
            }
            catch (ArgumentException)
            {
                Console.WriteLine(args[0] + "为无效运算符");
                Environment.Exit(1);
            }

            decimal[] numberArray = new decimal[args.Length - 1];

            int i = 1;
            while (i < args.Length)
            {
                numberArray[i - 1] = convertToDecimal(args[i]);
                i++;
            }

            switch (operactor)
            {
                case Operactor.add:
                    doCounting(numberArray,add);
                    break;
                case Operactor.minus:
                    doCounting(numberArray,minus);
                    break;
                case Operactor.multiply:
                    doCounting(numberArray,multiply);
                    break;
                case Operactor.dividedBy:
                    doCounting(numberArray,divideBy);
                    break;
                case Operactor.pow:
                    doCounting(numberArray,pow);
                    break;
                case Operactor.sqrt:
                    doCounting(numberArray,sqrt);
                    break;
                default:
                    break;
            }
        }

        static decimal convertToDecimal(String val)
        {
            decimal num = 0m;
            try
            {
                num = Convert.ToDecimal(val);
            }
            catch (FormatException )
            {
                Console.WriteLine("尝试将字符串" + val + "转化为decimal类型时,抛出FormatException异常.");
                Environment.Exit(1);
            }

            return num;
        }

        enum Operactor
        {
            add,         //加
            minus,       //减
            multiply,    //乘
            dividedBy,   //除以
            pow,         //乘方
            sqrt         //开平方
        }

        static void add(params decimal[] num)
        {
            foreach (decimal d in num)
                Console.WriteLine(d);
            decimal result = 0;
            foreach (decimal d in num)
                result += d;
            Console.WriteLine("以上数字的相加结果为" + result);
        }

        static void minus(params decimal[] num)
        {
            foreach (decimal d in num)
                Console.WriteLine(d);
            decimal result = num[0] * 2;
            foreach (decimal d in num)
                result -= d;
            Console.WriteLine("以上数字的相减结果为" + result);
        }

        static void multiply(params decimal[] num)
        {
            foreach (decimal d in num)
                Console.WriteLine(d);
            decimal result = 1;
            foreach (decimal d in num)
                result *= d;
            Console.WriteLine("以上数字的相乘结果为" + result);
        }

        static void divideBy(params decimal[] num)
        {
            foreach (decimal d in num)
                Console.WriteLine(d);
            decimal result = 1;
            foreach (decimal d in num)
                result /= d;
            Console.WriteLine("以上数字的相除结果为" + result);
        }

        static void pow(params decimal[] num)
        {
            int i = 1;
            while (i < num.Length)
            {
                Console.WriteLine(num[i - 1] + "的" + num[i] + "次幂为" + Math.Pow((double)num[i - 1],(double)num[i]));
                i += 2;
            }
            if (num.Length % 2 != 0)
                Console.WriteLine("将不对" + num[num.Length - 1] + "进行任何操作.");
        }

        static void sqrt(params decimal[] num)
        {
            foreach (decimal d in num)
                Console.WriteLine(d + "的算数平方根是" + Math.Sqrt((double)d));
        }

        static void PrintHelp()
        {
            string[] array = new string[8];
            array[0] = "获得一串数字的计算结果。\n";
            array[1] = "ConsoleCount [add | minus | multiply | dividedBy | pow | sqrt] [...]\n";
            array[2] = "add       返回后面一串数字的和。";
            array[3] = "minus     返回后面一串数字的差。";
            array[4] = "multiply  返回后面一串数字的积。";
            array[5] = "dividedBy 返回后面一串数字的商。";
            array[6] = "pow       返回n的n+1次幂。";
            array[7] = "sqrt      返回后面一串数字的正平方根。";

            foreach (string str in array)
                Console.WriteLine(str);

            Environment.Exit(0);
        }
    }
}
